package com.neusoft.cmsprojects.util;

import java.util.Collection;
import java.util.List;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.neusoft.cmsprojects.model.Privilege;
import com.neusoft.cmsprojects.service.PrivilegeService;


public class InitLisenter implements ServletContextListener {

	/**
	 * 启动准备数据
	 */
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		
		
		//取得Spring容器里面的bean值，这样才能保证是在同一个容器里面
		//如果不是则会引发一系列的错误
		ApplicationContext ac = WebApplicationContextUtils
				.getWebApplicationContext(sce.getServletContext());
		PrivilegeService privilegeService = (PrivilegeService) ac
				.getBean("privilegeservice");

		// 准备数据：topPrivilegeList
		List<Privilege> topPrivilegeList = privilegeService.findTopPrivilege();
		sce.getServletContext().setAttribute("topPrivilegeList",topPrivilegeList);
		System.out.println("------------> 已准备数据topPrivilegeList <------------");

		// 准备数据：allPrivilegeUrls
		Collection<String> allPrivilegeUrls = privilegeService.getAllPrivilegeUrls();
		sce.getServletContext().setAttribute("allPrivilegeUrls",allPrivilegeUrls);
		System.out.println("------------> 已准备数据allPrivilegeUrls <------------");

	}
	
	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		
	}


}
